import { Node } from '../core/node.js';
import { Sprite } from '../nodes/sprite.js';
import { eventEmitter } from '../util/eventEmitter.js';

export class AnimationClip extends Node {
    constructor(options = {}) {
        super();

        this.loop = typeof options.loop == "boolean" ? options.loop : true;
        this.fps = options.fps || 60;
        this.isOver = false;

        this._frames = [];
        this._time = 0;
        this._prevSprite = null;
        this._curIndex = 0;
    }

    set fps(value) {
        this._fps = value;
        this._interval = 1000 / value;
    }

    get fps() {
        return this._fps;
    }

    get playRate() {
        if (this._frames.length == 0) {
            return 0;
        }
        else {
            return (this._curIndex + 1) / this._frames.length;
        }
    }

    //添加一帧
    addFrame(sprite, frameIndex) {
        if (sprite) {
            //新增的默认不显示，后面在播放里打开显示
            sprite.visible = false;

            this.add(sprite);
        }

        if (frameIndex) {
            if (frameIndex < this._frames.length) {
                this._frames[frameIndex] = sprite;
            }
            else {
                while (this._frames.length < frameIndex) {
                    this._frames.push(null);
                }

                this._frames.push(sprite);
            }
        }
        else {
            this._frames.push(sprite);
        }
    }

    //添加多帧依据横条多图
    addFrameByRowImageUrl(url, cellCount) {
        let cellWidth = 1 / cellCount;

        for (let i = 0; i < cellCount; i++) {
            let uv = [
                i * cellWidth, 1,
                (i + 1) * cellWidth, 1,
                (i + 1) * cellWidth, 0,
                i * cellWidth, 0,
            ];

            this.addFrame(new Sprite({ url, uv }))
        }
    }

    //播放图片序列
    play(frameDelta) {
        if (this.isOver || this._frames.length == 0) return;

        this._time += frameDelta;

        let index = Math.floor(this._time / this._interval);

        if (index >= this._frames.length) {
            if (this.loop) {
                index = 0;
                this._time %= this._interval;
            }
            else {
                index = this._frames.length - 1;
                this.isOver = true;
            }
        }

        let sprite = this._frames[index];

        if (sprite) {
            sprite.visible = true;

            if (this._prevSprite && this._prevSprite != sprite) {
                this._prevSprite.visible = false;
            }

            this._prevSprite = sprite;
        }

        if (this._curIndex != index) {
            this._curIndex = index;

            this.emit("play" + index);
        }
    }

    reset() {
        for (let item of this.children) {
            item.visible = false;
        }

        this.isOver = false;
        this._time = 0;
    }

    //停止所有播放
    stop() {
        for (let item of this.children) {
            item.visible = false;
        }

        this.isOver = true;
    }
}

eventEmitter(AnimationClip);